package com.example.dormitory_manager.controller;


import com.example.dormitory_manager.entity.DormitoryAdmin;
import com.example.dormitory_manager.form.RuleForm;
import com.example.dormitory_manager.form.SearchForm;
import com.example.dormitory_manager.service.DormitoryAdminService;
import com.example.dormitory_manager.utils.ResultVOUtil;
import com.example.dormitory_manager.vo.PageVo;
import com.example.dormitory_manager.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.relation.RoleResult;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-10-02
 */
@RestController
@RequestMapping("/dormitoryAdmin")
public class DormitoryAdminController {
    @Autowired
    private DormitoryAdminService dormitoryAdminService;

    @GetMapping("/login")
    public ResultVo login(RuleForm ruleForm) {
        ResultVo resultVo = this.dormitoryAdminService.login(ruleForm);
        return resultVo;
    }

    @PostMapping("/save")
    public ResultVo save(@RequestBody DormitoryAdmin dormitoryAdmin){
        boolean save =this.dormitoryAdminService.save(dormitoryAdmin);
        if(!save) return ResultVOUtil.fail();
        return  ResultVOUtil.success("成功");
    }

    @GetMapping("/list/{page}/{size}")
    public ResultVo list(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        PageVo pageVo=this.dormitoryAdminService.list(page,size);
        return ResultVOUtil.success(pageVo);
    }

    @GetMapping("/search")
    public ResultVo search(SearchForm searchForm){
        PageVo pageVo=this.dormitoryAdminService.search(searchForm);
        return  ResultVOUtil.success(pageVo);
    }

    @GetMapping("/findById/{id}")
    public ResultVo findById(@PathVariable("id") Integer id){
        DormitoryAdmin dormitoryAdmin =this.dormitoryAdminService.getById(id);
        return  ResultVOUtil.success(dormitoryAdmin);
    }

    @PutMapping("/update")
    public ResultVo update(@RequestBody DormitoryAdmin dormitoryAdmin){
       boolean update=this.dormitoryAdminService.updateById(dormitoryAdmin);
       if(!update) return  ResultVOUtil.fail();
       return  ResultVOUtil.success(null);
    }

    @DeleteMapping("/deleteById/{id}")
    public ResultVo deleteById(@PathVariable("id") Integer id){
        boolean remove =this.dormitoryAdminService.removeById(id);
        if(!remove) return ResultVOUtil.fail();
        return  ResultVOUtil.success(null);
    }
}

